package cn.iocoder.yudao.module.erp.controller.admin.dpitem.dpitemgz;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.erp.controller.admin.dpitem.dpitemgz.vo.DpItemGzPageReqVO;
import cn.iocoder.yudao.module.erp.controller.admin.dpitem.dpitemgz.vo.DpItemGzRespVO;
import cn.iocoder.yudao.module.erp.controller.admin.dpitem.dpitemgz.vo.DpItemGzSaveReqVO;
import cn.iocoder.yudao.module.erp.dal.dataobject.dpitem.DpItemGzDO;
import cn.iocoder.yudao.module.erp.service.dpitem.DpItemGzService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.io.IOException;
import java.util.List;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - ERP广州制单货号详情")
@RestController
@RequestMapping("/erp/dp-item-gz")
@Validated
public class DpItemGzController {

    @Resource
    private DpItemGzService dpItemGzService;

    @PostMapping("/create")
    @Operation(summary = "创建ERP广州制单货号详情")
    // @PreAuthorize("@ss.hasPermission('erp:dp-item-gz:create')")
    public CommonResult<Long> createDpItemGz(@Valid @RequestBody DpItemGzSaveReqVO createReqVO) {
        return success(dpItemGzService.createDpItemGz(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新ERP广州制单货号详情")
    // @PreAuthorize("@ss.hasPermission('erp:dp-item-gz:update')")
    public CommonResult<Boolean> updateDpItemGz(@Valid @RequestBody DpItemGzSaveReqVO updateReqVO) {
        dpItemGzService.updateDpItemGz(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除ERP广州制单货号详情")
    @Parameter(name = "id", description = "编号", required = true)
    // @PreAuthorize("@ss.hasPermission('erp:dp-item-gz:delete')")
    public CommonResult<Boolean> deleteDpItemGz(@RequestParam("id") Long id) {
        dpItemGzService.deleteDpItemGz(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得ERP广州制单货号详情")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    // @PreAuthorize("@ss.hasPermission('erp:dp-item-gz:query')")
    public CommonResult<DpItemGzRespVO> getDpItemGz(@RequestParam("id") Long id) {
        DpItemGzDO dpItemGz = dpItemGzService.getDpItemGz(id);
        return success(BeanUtils.toBean(dpItemGz, DpItemGzRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得ERP广州制单货号详情分页")
    // @PreAuthorize("@ss.hasPermission('erp:dp-item-gz:query')")
    public CommonResult<PageResult<DpItemGzRespVO>> getDpItemGzPage(@Valid DpItemGzPageReqVO pageReqVO) {
        PageResult<DpItemGzDO> pageResult = dpItemGzService.getDpItemGzPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, DpItemGzRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出ERP广州制单货号详情 Excel")
    // @PreAuthorize("@ss.hasPermission('erp:dp-item-gz:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportDpItemGzExcel(@Valid DpItemGzPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<DpItemGzDO> list = dpItemGzService.getDpItemGzPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "ERP广州制单货号详情.xls", "数据", DpItemGzRespVO.class,
                        BeanUtils.toBean(list, DpItemGzRespVO.class));
    }

}